package org.mao.util;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class SystemConfig {

	private static Logger logger = Logger.getLogger(SystemConfig.class);

	private String forumSavePath;

	public String getForumSavePath() {
		return forumSavePath;
	}

	public void setForumSavePath(String forumSavePath) {
		this.forumSavePath = forumSavePath;
	}

	private static SystemConfig systemConfig = null;

	private SystemConfig() {

	}

	public static SystemConfig getInstance() {
		if (systemConfig == null) {
			systemConfig = new SystemConfig();
			loanConfig();
		}
		return systemConfig;
	}

	private static void loanConfig() {
		try {
			InputStream inputStream = SystemConfig.class.getResourceAsStream("/conf/systemconfig.properties");
			Properties prop = new Properties();
			prop.load(new InputStreamReader(inputStream, "UTF-8"));

			Field fields[] = SystemConfig.class.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];
				String porName = field.getName();
				String porType = field.getType().toString();

				String val = prop.getProperty(porName);
				if (StringUtils.isNotBlank(val)) {

					switch (porType) {
					case "class java.lang.Integer":
						field.set(systemConfig, Integer.parseInt(val.toString()));
						break;
					case "class java.lang.String":
						field.set(systemConfig, val);
						break;
					case "class java.lang.Boolean":
						field.set(systemConfig, Boolean.valueOf(val.toString()));
						break;
					case "class java.lang.Double":
						field.set(systemConfig, Double.valueOf(val.toString()));
						break;
					case "class java.lang.Byte":
						field.set(systemConfig, Byte.valueOf(val.toString()));
						break;
					case "class java.lang.Long":
						field.set(systemConfig, Long.valueOf(val.toString()));
						break;
					default:
						break;
					}
				}
			}
		} catch (Exception e) {
			logger.error("", e);
		}
	}

}
